﻿using System;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Miris.HumanMilkAnalyser.DataModel;
using Miris.HumanMilkAnalyser.Service;
using Miris.HumanMilkAnalyser.Service.Interface;
using Miris.HumanMilkAnalyser.Service.Shared;
using Miris.HumanMilkAnalyser.WPF.Model;
using Miris.HumanMilkAnalyser.WPF.Shared;

namespace Miris.HumanMilkAnalyser.WPF.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class AnalysisResultMappingViewModel : PatientViewModel
    {
        public const string GoToNextPageMessageToken = "AnalysisResultMappingGoToNextPageMessage";
        public MilkSample CurrentMilkSample { get; set; }
        public MilkStage CurrentMilkStage { get; set; }
        public AnalysisResult CurrentAnalysisResult { get; set; }

        /// <summary>
        /// Initializes a new instance of the AnalysisResultMappingViewModel class.
        /// </summary>
        public AnalysisResultMappingViewModel(IDataService dataService)
            : base(dataService) { }

        public override void ResetProperties()
        {
            base.ResetProperties();

            MotherName = string.Empty;
            MotherAge = null;
            MotherHeight = null;
            MotherWeight = null;
            MotherBMI = null;
            MotherOpenMilkDate = GetDefaultDate();
            MotherDeliveryMode = "自然顺产";
            MotherEmotion = string.Empty;
            MotherBadHabit = string.Empty;

            BabyName = string.Empty;
            BabyBirthday = GetDefaultDate();
            IsGirl = true;
            IsBabyNotPrematureDelivery = true;
            BabyHeight = null;
            BabyWeight = null;
            BabyHeadCircumference = null;
            BabyBirthHeight = null;
            BabyBirthWeight = null;
            BabyBirthHeadCircumference = null;
            Apgar = null;

            IsNewData = true;
        }

        /// <summary>
        /// Set the analysis result properties based on provided MainPageViewModel.
        /// </summary>
        /// <param name="mainPageViewModel">Instance of MainPageViewModel.</param>
        public void SetAnalysisResultProperties(MainPageViewModel mainPageViewModel)
        {
            CurrentMilkSample = mainPageViewModel.CurrentMilkSample;
            CurrentMilkStage = mainPageViewModel.CurrentMilkStage;
            CurrentAnalysisResult = mainPageViewModel.CurrentAnalysisResult;
            SampleNumber = mainPageViewModel.CurrentAnalysisResult.SampleNumber;

            Number = PatientService.GetPatientNumber();
        }

        /// <summary>
        /// The <see cref="SampleNumber" /> property's name.
        /// </summary>
        public const string SampleNumberPropertyName = "SampleNumber";

        private string _sampleNumber = string.Empty;

        /// <summary>
        /// Sets and gets the SampleNumber property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string SampleNumber
        {
            get
            {
                return _sampleNumber;
            }

            set
            {
                if (_sampleNumber == value)
                {
                    return;
                }

                RaisePropertyChanging(SampleNumberPropertyName);
                _sampleNumber = value;
                RaisePropertyChanged(SampleNumberPropertyName);
            }
        }

        private RelayCommand _nextCommand;

        /// <summary>
        /// Gets the NextCommand.
        /// </summary>
        public RelayCommand NextCommand
        {
            get
            {
                return _nextCommand ?? (_nextCommand = new RelayCommand(
                    ExecuteNextCommand,
                    CanExecuteNextCommand));
            }
        }

        private void ExecuteNextCommand()
        {
            try
            {
                if (AddOrUpdatePatient())
                {
                    CurrentAnalysisResult.PatientNumber = Number;

                    // Send GoToNextPageMessage to AnalysisResultMappingPage.
                    Messenger.Default.Send<AnalysisResultMappingViewModel>(this, GoToNextPageMessageToken);
                }
            }
            catch (Exception ex)
            {
                RequestDisplayErrorMessage(ex.Message);
            }
        }

        private bool CanExecuteNextCommand()
        {
            return CanExecuteCommand();
        }
    }
}